package io.terminus.spzx.manager.job;

import cn.hutool.core.date.DateUtil;
import io.terminus.spzx.manager.mapper.OrderInfoMapper;
import io.terminus.spzx.manager.mapper.OrderStatisticsMapper;
import io.terminus.spzx.model.entity.order.OrderStatistics;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * @Author lichunlong lcl253137@alibaba-inc.com
 * @Desc 订单统计定时任务
 * @Date 2024-10-14
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class OrderStatisticsTask {

    private final OrderInfoMapper orderInfoMapper;

    private final OrderStatisticsMapper orderStatisticsMapper;

    @Scheduled(cron = "0 0 2 * * ?")
    public void helloWorldTask() {
        // 获取前一天的订单数据
        String createTime = DateUtil.offsetDay(new Date(), -1).toString("yyyy-MM-dd");
        OrderStatistics orderStatistics = orderInfoMapper.selectOrderStatistics(createTime);
        if (Objects.nonNull(orderStatistics)) {
            orderStatisticsMapper.insert(orderStatistics);
        }
    }
}
